/////////////////////////////////////////////////////////////////////////////
// Name:        ax_menu_colour.cpp
// Purpose:     Colour selection menu
// Author:      Julian Smart
// Modified by:
// Created:     2009-03-20
// RCS-ID:
// Copyright:   (c) Julian Smart
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

#include "ax/ax_menu_choice.h"
#include "ax/ax_menu_colour.h"

#if 0

/**
    @class AxColourMenuActivator

    Top-level item activator for a modal choice menu item.

    @category{ax}

 */

class AxColourMenuActivator: public AxAutoUpdateActivator
{
public:
    
    DECLARE_DYNAMIC_CLASS( AxColourMenuActivator )

public:
// Constructors

    AxColourMenuActivator() { Init(); }

    AxColourMenuActivator(
        const wxString& labelSpec,
        const wxString& descriptionSpec);

    AxColourMenuActivator(const AxColourMenuActivator& activator) { Init(); Copy(activator); }

    void Init();

    void Copy(const AxColourMenuActivator& activator);

    virtual AxActivator* Clone() const { return new AxColourMenuActivator(* this); }

    /// Called when the user activates a menu item by pressing return or double-clicking
    virtual AxMenu* Activate(AxMenuController* controller, AxMenuItem* item);

    /// Create menu label or description
    virtual wxString CreateString(const wxString& spec, bool isLabel);

    AxColourMenuItem* GetColourMenuItem() const;

protected:
};

#endif

IMPLEMENT_DYNAMIC_CLASS(AxColourMenuItem, AxChoiceMenuItem)

AxColourMenuItem::AxColourMenuItem(const wxString& label, const wxString& description,
        const wxString& id,
        const wxColour& colour)
{
    Create(label, description, id, colour);
}

bool AxColourMenuItem::Create(const wxString& label, const wxString& description,
        const wxString& id,
        const wxColour& colour)
{
    wxArrayString colours;

    colours.Add(wxT("Default"));

    colours.Add(wxT("Aquamarine"));
    colours.Add(wxT("Black"));
    colours.Add(wxT("Blue"));
    colours.Add(wxT("Blue violet"));
    colours.Add(wxT("Brown"));
    colours.Add(wxT("Cadet blue"));
    colours.Add(wxT("Coral"));
    colours.Add(wxT("Cornflower blue"));
    colours.Add(wxT("Cyan"));
    colours.Add(wxT("Dark grey"));

    colours.Add(wxT("Dark green"));
    colours.Add(wxT("Dark olive green"));
    colours.Add(wxT("Dark orchid"));
    colours.Add(wxT("Dark slate blue"));
    colours.Add(wxT("Dark slate grey"));
    colours.Add(wxT("Dark turquoise"));
    colours.Add(wxT("Dim grey"));
    colours.Add(wxT("Firebrick"));
    colours.Add(wxT("Forest green"));
    colours.Add(wxT("Gold"));
    colours.Add(wxT("Goldenrod"));
    colours.Add(wxT("Grey"));
    colours.Add(wxT("Green"));
    colours.Add(wxT("Green yellow"));
    colours.Add(wxT("Indian red"));
    colours.Add(wxT("Khaki"));
    colours.Add(wxT("Light blue"));
    colours.Add(wxT("Light grey"));
    colours.Add(wxT("Light steel blue"));
    colours.Add(wxT("Lime green"));
    colours.Add(wxT("Light magenta"));
    colours.Add(wxT("Magenta"));
    colours.Add(wxT("Maroon"));
    colours.Add(wxT("Medium aquamarine"));
    colours.Add(wxT("Medium grey"));
    colours.Add(wxT("Medium blue"));
    colours.Add(wxT("Medium forest green"));
    colours.Add(wxT("Medium goldenrod"));
    colours.Add(wxT("Medium orchid"));
    colours.Add(wxT("Medium sea green"));
    colours.Add(wxT("Medium slate blue"));
    colours.Add(wxT("Medium spring green"));
    colours.Add(wxT("Medium turquoise"));
    colours.Add(wxT("Medium violet red"));
    colours.Add(wxT("Midnight blue"));
    colours.Add(wxT("Navy"));
    colours.Add(wxT("Orange"));
    colours.Add(wxT("Orange red"));
    colours.Add(wxT("Orchid"));
    colours.Add(wxT("Pale green"));
    colours.Add(wxT("Pink"));
    colours.Add(wxT("Plum"));
    colours.Add(wxT("Purple"));
    colours.Add(wxT("Red"));
    colours.Add(wxT("Salmon"));
    colours.Add(wxT("Sea green"));
    colours.Add(wxT("Sienna"));
    colours.Add(wxT("Sky blue"));
    colours.Add(wxT("Slate blue"));
    colours.Add(wxT("Spring green"));
    colours.Add(wxT("Steel blue"));
    colours.Add(wxT("Tan"));
    colours.Add(wxT("Thistle"));
    colours.Add(wxT("Turquoise"));
    colours.Add(wxT("Violet"));
    colours.Add(wxT("Violet red"));
    colours.Add(wxT("Wheat"));
    colours.Add(wxT("White"));
    colours.Add(wxT("Yellow"));
    colours.Add(wxT("Yellow green"));

    wxArrayString ids, descriptions;

    size_t i;
    for (i = 0; i < colours.GetCount(); i++)
    {
        wxString id = colours[i].Upper();
        wxString descr = wxString::Format(_("Select colour %s."), colours[i].c_str());

        ids.Add(id);
        descriptions.Add(descr);
    }

    int idx = 0;
    if (colour.IsOk())
    {
        wxString defaultColour = wxTheColourDatabase->FindName(colour);
        int pos = ids.Index(defaultColour);
        if (pos != -1)
            idx = pos;
    }

    AxChoiceMenuItem::Create(label, description, id, colours, ids, descriptions, idx);

    return true;
}

void AxColourMenuItem::Copy(const AxColourMenuItem& item)
{
    AxChoiceMenuItem::Copy(item);
    
    m_colour = item.m_colour;
}

#if 0
wxString AxColourMenuItem::CreateString(const wxString& spec, bool WXUNUSED(isLabel))
{
    wxString value;
    if (m_selection != -1 && m_selection < (int) m_labels.GetCount())
        value = m_labels[m_selection];

    if (value.IsEmpty())
        value = _("none");
    wxString str = spec;
    str.Replace(wxT("%VALUE%"), value);
    return str;
}
#endif

void AxColourMenuItem::SetColour(const wxColour& colour)
{
    int idx = 0;
    if (colour.IsOk())
    {
        wxString defaultColour = wxTheColourDatabase->FindName(colour);
        int pos = GetIds().Index(defaultColour);
        if (pos != -1)
            idx = pos;
    }

    AxChoiceMenuItem::SetSelection(idx);
}

/// Set current selection
void AxColourMenuItem::SetSelection(int i)
{
    AxChoiceMenuItem::SetSelection(i);

    wxString id = GetSelectionId();

    if (i == 0)
    {
        m_colour = wxColour();
    }
    else
    {
        wxColour col = wxTheColourDatabase->Find(id);
        if (col.IsOk())
            m_colour = col;
    }
}

#if 0
/*
 * AxColourMenuActivator implementation.
 * Sets the value of a string variable when selected.
 */

IMPLEMENT_DYNAMIC_CLASS( AxColourMenuActivator, AxAutoUpdateActivator )

AxColourMenuActivator::AxColourMenuActivator(
                  const wxString& labelSpec,
                  const wxString& descriptionSpec):
  AxAutoUpdateActivator(labelSpec, descriptionSpec)
{
    Init();
}

void AxColourMenuActivator::Init()
{
}

AxColourMenuItem* AxColourMenuActivator::GetColourMenuItem() const
{
    return wxDynamicCast(GetMenuItem(), AxColourMenuItem);
}

void AxColourMenuActivator::Copy(const AxColourMenuActivator& activator)
{
    AxAutoUpdateActivator::Copy(activator);
}

AxMenu* AxColourMenuActivator::Activate(AxMenuController* controller, AxMenuItem* item)
{
    AxColourMenuItem* colourItem = GetColourMenuItem();
    if (!colourItem)
        return NULL;

    wxString defaultId = colourItem->GetSelectionId();

    AxModalChoiceMenu menu(controller, item,
        _("Choice"),
        _("Please choose a colour from this list."),
        colourItem->GetLabels(), colourItem->GetIds(), colourItem->GetDescriptions(),
        defaultId);

    wxString id = menu.ShowModal();

    {
        if (id != AxIdentifierCancel)
        {
            int idx = colourItem->GetIds().Index(id);
            if (idx != -1)
            {
                colourItem->SetSelection(idx);

                if (item->GetValidator())
                    item->GetValidator()->TransferDataFromMenu();

                UpdateItem(controller, item);

                wxString result = colourItem->GetSelectionLabel();

                if (!item->GetId().IsEmpty())
                {
                    wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, AxGetId(item->GetId()));
                    event.SetInt(idx);
                    event.SetString(result);
                    event.SetEventObject(item);

                    item->ProcessEvent(event);
                }
            }
        }
    }

    return NULL;
}

wxString AxColourMenuActivator::CreateString(const wxString& spec, bool isLabel)
{
    AxColourMenuItem* colourItem = GetColourMenuItem();
    if (colourItem)
        return colourItem->CreateString(spec, isLabel);
    else
        return spec;
}

#endif

IMPLEMENT_DYNAMIC_CLASS( AxColourValidator, AxValidator )

// Override to pass a value from the UI to a data structure
bool AxColourValidator::TransferDataFromMenu()
{
    AxColourMenuItem* colourItem = wxDynamicCast(GetMenuItem(), AxColourMenuItem);
    if (colourItem)
    {
        if (m_colourPtr)
            * m_colourPtr = colourItem->GetColour();
    }

    return true;
}

// Override to pass a value from a data structure to the UI
bool AxColourValidator::TransferDataToMenu()
{
    AxColourMenuItem* colourItem = wxDynamicCast(GetMenuItem(), AxColourMenuItem);
    if (colourItem)
    {
        if (m_colourPtr)
            colourItem->SetColour(* m_colourPtr);
    }

    return true;
}
